﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;

namespace Terraria.ModLoader.IO
{
	// Token: 0x020002E2 RID: 738
	public class UploadFile
	{
		// Token: 0x06001C53 RID: 7251 RVA: 0x000143D5 File Offset: 0x000125D5
		public UploadFile()
		{
			this.ContentType = "application/octet-stream";
		}

		// Token: 0x170001D5 RID: 469
		// (get) Token: 0x06001C54 RID: 7252 RVA: 0x000143E8 File Offset: 0x000125E8
		// (set) Token: 0x06001C55 RID: 7253 RVA: 0x000143F0 File Offset: 0x000125F0
		public string Name { get; set; }

		// Token: 0x170001D6 RID: 470
		// (get) Token: 0x06001C56 RID: 7254 RVA: 0x000143F9 File Offset: 0x000125F9
		// (set) Token: 0x06001C57 RID: 7255 RVA: 0x00014401 File Offset: 0x00012601
		public string Filename { get; set; }

		// Token: 0x170001D7 RID: 471
		// (get) Token: 0x06001C58 RID: 7256 RVA: 0x0001440A File Offset: 0x0001260A
		// (set) Token: 0x06001C59 RID: 7257 RVA: 0x00014412 File Offset: 0x00012612
		public string ContentType { get; set; }

		// Token: 0x170001D8 RID: 472
		// (get) Token: 0x06001C5A RID: 7258 RVA: 0x0001441B File Offset: 0x0001261B
		// (set) Token: 0x06001C5B RID: 7259 RVA: 0x00014423 File Offset: 0x00012623
		public byte[] Content { get; set; }

		// Token: 0x06001C5C RID: 7260 RVA: 0x004350F8 File Offset: 0x004332F8
		public static byte[] UploadFiles(string address, IEnumerable<UploadFile> files, NameValueCollection values)
		{
			WebRequest webRequest = WebRequest.Create(address);
			webRequest.Method = "POST";
			string text = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
			webRequest.ContentType = "multipart/form-data; boundary=" + text;
			text = "--" + text;
			using (Stream requestStream = webRequest.GetRequestStream())
			{
				UploadFile.WriteValues(requestStream, values, text);
				UploadFile.WriteFiles(requestStream, files, text);
				byte[] bytes = Encoding.ASCII.GetBytes(text + "--");
				requestStream.Write(bytes, 0, bytes.Length);
			}
			byte[] result;
			using (WebResponse response = webRequest.GetResponse())
			{
				using (Stream responseStream = response.GetResponseStream())
				{
					using (MemoryStream memoryStream = new MemoryStream())
					{
						responseStream.CopyTo(memoryStream);
						result = memoryStream.ToArray();
					}
				}
			}
			return result;
		}

		// Token: 0x06001C5D RID: 7261 RVA: 0x00435234 File Offset: 0x00433434
		public static byte[] GetUploadFilesRequestData(IEnumerable<UploadFile> files, NameValueCollection values)
		{
			string text = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
			text = "--" + text;
			byte[] result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				UploadFile.WriteValues(memoryStream, values, text);
				UploadFile.WriteFiles(memoryStream, files, text);
				byte[] bytes = Encoding.ASCII.GetBytes(text + "--");
				memoryStream.Write(bytes, 0, bytes.Length);
				result = memoryStream.ToArray();
			}
			return result;
		}

		// Token: 0x06001C5E RID: 7262 RVA: 0x004352D8 File Offset: 0x004334D8
		private static void WriteValues(Stream requestStream, NameValueCollection values, string boundary)
		{
			if (values == null)
			{
				return;
			}
			foreach (object obj in values.Keys)
			{
				string text = (string)obj;
				byte[] bytes = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
				requestStream.Write(bytes, 0, bytes.Length);
				bytes = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", text, Environment.NewLine));
				requestStream.Write(bytes, 0, bytes.Length);
				bytes = Encoding.UTF8.GetBytes(values[text] + Environment.NewLine);
				requestStream.Write(bytes, 0, bytes.Length);
			}
		}

		// Token: 0x06001C5F RID: 7263 RVA: 0x004353A0 File Offset: 0x004335A0
		private static void WriteFiles(Stream requestStream, IEnumerable<UploadFile> files, string boundary)
		{
			if (files == null)
			{
				return;
			}
			foreach (UploadFile uploadFile in files)
			{
				byte[] bytes = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
				requestStream.Write(bytes, 0, bytes.Length);
				bytes = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", uploadFile.Name, uploadFile.Filename, Environment.NewLine));
				requestStream.Write(bytes, 0, bytes.Length);
				bytes = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", uploadFile.ContentType, Environment.NewLine));
				requestStream.Write(bytes, 0, bytes.Length);
				requestStream.Write(uploadFile.Content, 0, uploadFile.Content.Length);
				bytes = Encoding.ASCII.GetBytes(Environment.NewLine);
				requestStream.Write(bytes, 0, bytes.Length);
			}
		}
	}
}
